package com.jeesite.modules.jydq.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.jydq.entity.JydqCategory;
import com.jeesite.modules.jydq.service.JydqCategoryService;

/**
 * 商品分类表Controller
 * @author dong
 * @version 2025-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/jydq/category")
public class JydqCategoryController extends BaseController {

	@Autowired
	private JydqCategoryService jydqCategoryService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public JydqCategory get(String id, boolean isNewRecord) {
		return jydqCategoryService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
//	@RequiresPermissions("jydq:category:view")
	@RequestMapping(value = {"list", ""})
	public String list(JydqCategory jydqCategory, Model model) {
		model.addAttribute("jydqCategory", jydqCategory);
		return "modules/jydq/jydqCategoryList";
	}
	
	/**
	 * 查询列表数据
	 */
//	@RequiresPermissions("jydq:category:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<JydqCategory> listData(JydqCategory jydqCategory, HttpServletRequest request, HttpServletResponse response) {
		jydqCategory.setPage(new Page<>(request, response));
		Page<JydqCategory> page = jydqCategoryService.findPage(jydqCategory);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
//	@RequiresPermissions("jydq:category:view")
	@RequestMapping(value = "form")
	public String form(JydqCategory jydqCategory, Model model) {
		model.addAttribute("jydqCategory", jydqCategory);
		return "modules/jydq/jydqCategoryForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("jydq:category:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated JydqCategory jydqCategory) {
		jydqCategoryService.save(jydqCategory);
		return renderResult(Global.TRUE, text("保存商品分类表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("jydq:category:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(JydqCategory jydqCategory) {
		jydqCategoryService.delete(jydqCategory);
		return renderResult(Global.TRUE, text("删除商品分类表成功！"));
	}

	/**
	 * 查询下拉框选项
	 */
	@RequestMapping({"treeData"})
	@ResponseBody
	public List<Map<String, Object>> treeData(JydqCategory jydqCategory) {
		List<Map<String, Object>> list = new ArrayList<>();
		List<JydqCategory> list1 = jydqCategoryService.findList(jydqCategory);
		for (JydqCategory data : list1) {
			HashMap<String, Object> map = new HashMap<>();
			map.put("id",data.getId());
			map.put("name",data.getName());
			list.add(map);
		}
		return list;
	}
}